<?php
/**
 * Upload.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   17-9-14 下午3:18
 */

namespace Common;

class Upload
{
    /**
     * @param string      $path         PathConst
     * @param array|mixed $image_status PhotoImage::checkImage|PhotoImage::checkImageByUrl|base64
     * @param int|array   $size         裁剪尺寸    0:原图
     * @param int         $itemid       项目id     0:按时间生成路径
     * @param array       $extra        quality   裁剪质量 0 - 100
     *                                  position  位置 top|middle|bottom
     *                                  height    限制高度
     *                                  watermark 水印 true|false
     * @return array|bool
     */
    public static function saveImg($path, $image_status, $size = 100, $itemid = 0, $extra = [])
    {
        if ($image_status['code'] == 0) {
            $image = $image_status;
            if ($image['image_type'] == 'png') {
                $image_type = 'png';
            } elseif ($image['image_type'] == 'gif') {
                $image_type = 'gif';
            } else {
                $image_type = 'jpg';
            }
            $path_url = getSavePath($path, $itemid, $image_type, $size);
            if ($path_url['path']) {
                $img_info = $path_url;
            } else {
                $img_info = current($path_url);
            }
//            debug_log('imginfo:' . json_encode($img_info) . json_encode($size), JF_LOG_WARNING);
//            unset($path_url[0]);
            if (!is_dir(dirname($img_info['path']))) {
                mkdir(dirname($img_info['path']), 0755, true);
            }
//            file_put_contents($img_info['path'], $image['tmp_name']);
//            if (empty($path_url)) {
//                return [
//                    'status' => true,
//                    'thumb' => [
//                        0 => $img_info['url']
//                    ]
//                ];
//            }
//            $image_status = PhotoImage::checkImageByUrl($img_info['url']);
        }
//        $path_url = getSavePath($path, $itemid, $image_status['image_type'], $size);
//        if (!is_numeric(key($path_url))) {
//            $path_url = [$path_url];
//        }

        $image_cfg = [
            'base' => dirname($img_info['path']) . '/',
            'thumb' => [
                'source' => [
                    'url' => '{id}.jpg'
                ],
                ],
            ];
        if (count($size) == 1) {
            $size = [$size];
        }
        foreach ($size as $v) {
            $image_cfg['thumb'][$v] = [
                'width' => $v,
                'position' => isset($extra['position']) ? $extra['position'] : 'middle',
                'quality' => isset($extra['quality']) ? $extra['quality'] : 85,
                'watermark' => isset($extra['watermark']) ? $extra['watermark'] : false,
                'url' => '{id}_' . $v . '.jpg'
            ];
        }
        if (!empty($extra['height'])) {
            foreach ($extra['height'] as $k => $v) {
                $image_cfg['thumb'][$k]['height'] = $v;
            }
        }
        $image_cfg['id'] = $itemid;
        if (!empty($image_cfg)) {
            debug_log('imginfo:' . json_encode($image_cfg), JF_LOG_WARNING);
            $ret = PhotoImage::imageResize($image_cfg, $image_status);
            if ($ret['status'] == true) {
                return $img_info['url'];
            }
            return $ret;
        }

        return false;
    }
}